/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmCustomerBrowseDialog1.java
 *
 * Created on Apr 6, 2011, 10:24:32 AM
 */
package inventorymanagement;

import javax.swing.JOptionPane;
import DataAccess.TableAccess;
import java.awt.Dimension;
import java.awt.Toolkit;
import model.ColorTable;

/**
 *
 * @author Truongbv_b00929
 */
public class frmCustomerBrowseDialog1 extends javax.swing.JDialog {

    static frmProcedureBill code;

    /** Creates new form frmCustomerBrowseDialog1 */
    public frmCustomerBrowseDialog1(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(((int) screenSize.getWidth() - this.getWidth()) / 2,
                ((int) screenSize.getHeight() - this.getHeight()) / 2);
        refresh();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtSearchName = new javax.swing.JTextField();
        txtSearhCustomerID = new javax.swing.JTextField();
        btnSearchRefresh = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblCustomer = new ColorTable();
        btnSelect = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Customer Browse");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(51, 153, 255), null));

        jLabel1.setText("Customer ID:");

        jLabel2.setText("Customer Name:");

        txtSearchName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchNameKeyReleased(evt);
            }
        });

        txtSearhCustomerID.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearhCustomerIDKeyReleased(evt);
            }
        });

        btnSearchRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Symbol-Refresh.png"))); // NOI18N
        btnSearchRefresh.setText("Refresh");
        btnSearchRefresh.setPreferredSize(new java.awt.Dimension(100, 23));
        btnSearchRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchRefreshActionPerformed(evt);
            }
        });

        tblCustomer.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblCustomer.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblCustomerMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblCustomer);

        btnSelect.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Symbol-Check.png"))); // NOI18N
        btnSelect.setText("Select");
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtSearhCustomerID, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtSearchName))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSearchRefresh, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addComponent(btnSelect))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSearchRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(txtSearhCustomerID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(txtSearchName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addComponent(btnSelect)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblCustomerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCustomerMouseClicked
        // TODO add your handling code here:
        int row = tblCustomer.getSelectedRow();
        String str = tblCustomer.getValueAt(row, 0).toString();
        String str1 = tblCustomer.getValueAt(row, 1).toString();
        txtSearhCustomerID.setText(str);
        txtSearchName.setText(str1);
}//GEN-LAST:event_tblCustomerMouseClicked

    private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectActionPerformed
        int cusID = tblCustomer.getSelectedRow();
        if (cusID == -1) {
            JOptionPane.showMessageDialog(this, "You must choose a item");
            return;
        } else {
            int row = tblCustomer.getSelectedRow();
            code.txtCustomerID1.setText(tblCustomer.getValueAt(row, 0).toString());
            this.dispose();
        }
}//GEN-LAST:event_btnSelectActionPerformed

    private void txtSearhCustomerIDKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearhCustomerIDKeyReleased
        // TODO add your handling code here:
        String str = txtSearhCustomerID.getText();
        String sql = "";
//        if (str.equals("")) {
//            sql = "select CustomerID,CustomerName,Address from Customers";
//        }
//        if (interact.iCheckForm.checkNumber(str)) {
//            JOptionPane.showMessageDialog(this, "Invalid fomat", "Error", JOptionPane.ERROR_MESSAGE);
//            return;
//        } else {
//            sql = ("select CustomerID,CustomerName,Address from Customers where CustomerID=" + Integer.valueOf(str));
//        }

        if (interact.iCheckForm.checkNumber(str)) {
            JOptionPane.showMessageDialog(this, "Invalid fomat", "Error", JOptionPane.ERROR_MESSAGE);
            txtSearhCustomerID.setText("");
            return;
        }
        if (str.equals("")) {
            sql = "select * from Customers";
        } else {
            sql = "select * from Customers where CustomerID like '%" + str + "%'";
        }
        try {
            TableAccess.readToTableCus(sql, tblCustomer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_txtSearhCustomerIDKeyReleased

    private void txtSearchNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearchNameKeyReleased
        // TODO add your handling code here:
        String str = txtSearchName.getText();
        String sql = "";
        if (str.equals("")) {
            sql = "select * from Customers";
        } else {
            sql = ("select * from Customers where CustomerName like '%" + str + "%'");
        }
        try {
            TableAccess.readToTableCus(sql, tblCustomer);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }//GEN-LAST:event_txtSearchNameKeyReleased

    private void btnSearchRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchRefreshActionPerformed
        // TODO add your handling code here:
        this.refresh();
    }//GEN-LAST:event_btnSearchRefreshActionPerformed

    private void refresh() {
        txtSearhCustomerID.setText("");
        txtSearchName.setText("");
        String sql = ("select * from Customers");
        try {
            TableAccess.readToTableCus(sql, tblCustomer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                frmCustomerBrowseDialog1 dialog = new frmCustomerBrowseDialog1(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearchRefresh;
    private javax.swing.JButton btnSelect;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblCustomer;
    private javax.swing.JTextField txtSearchName;
    private javax.swing.JTextField txtSearhCustomerID;
    // End of variables declaration//GEN-END:variables
}
